package com.qf.xiaom.dao.impl;

import com.qf.xiaom.dao.CartDao;
import com.qf.xiaom.pojo.TbCart;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.List;

public class CartDaoImpl implements CartDao {

    @Override
    public TbCart findByUserIdAndGoodId(Long userId, Long goodId) {
        QueryRunner runner= DbUtils.getRunner();
        String sql="SELECT * from tb_cart where id =? and pid=?";
        TbCart cart=null;
        try {
            cart=runner.query(sql,new BeanHandler<>(TbCart.class),userId,goodId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return cart;
    }

    @Override
    public int updateCount(Long userId, Long goodId, int num) {
        QueryRunner runner=DbUtils.getRunner();
        String sql="UPDATE tb_cart set num =num+? where id =? and pid=?";
        try {
            return runner.update(sql,num,userId,goodId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addCart(Long userId, Long goodId, int num) {
        QueryRunner runner=DbUtils.getRunner();
        String sql="INSERT into tb_cart values (?,?,?,null)";
        try {
            int update=runner.update(sql,userId,goodId,num);
            return update;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<TbCart> findByUserId(Long userId) {
        QueryRunner runner=DbUtils.getRunner();

        return null;
    }

    @Override
    public void clearCart(Long userId) {




    }
}
